<?php
/**
 * FacetXML
 *
 * @package converters
 * @author Keith Alexander
 **/
class FacetXML extends ConvertableObject
{
	/**
	 * to_array
	 *
	 * @return array
	 * @author Keith Alexander
	 **/
	public function to_array($value=false)
	{
		if(!$value) $value = $this->value();
		
		$facets_array = array(
				'head'=> array(
					'query' => '',
					'fields' => array(),
					'top' => '',
					'output' => 'json',
					),
				'fields' => array(
					
					),
			
			); //this is the array we are creating
		
		$doc = new DOMDocument();
		$doc->loadXML(trim($value));
		$xpath = new DOMXPath($doc);
		$xpath->registerNamespace('f', 'http://schemas.talis.com/2007/facet-results#');
		$facets_array['head']['query'] = $xpath->query('//f:head/f:query')->item(0)->textContent;
		$facets_array['head']['top'] = $xpath->query('//f:head/f:top')->item(0)->textContent;
		$facets_array['head']['fields'] = explode(',',$xpath->query('//f:head/f:fields')->item(0)->textContent);
		
		$fields = $xpath->query('//f:fields/f:field');
		
		foreach($fields as $field)
		{
			$field_name = $field->getAttribute('name');
			$facets_array['fields'][$field_name] = array();
			$terms = $xpath->query('f:term', $field);
			foreach($terms as $term)
			{
				$term_array = array(
					'value'=> $term->getAttribute('value'),
					'number'=> $term->getAttribute('number'),
					'facet_uri' => $term->getAttribute('facet-uri'),
					'search_uri'=> $term->getAttribute('search-uri'),
					);
				$facets_array['fields'][$field_name][]= $term_array;
			}
		}
		require_once 'FacetArray.class.php';
		return new FacetArray($facets_array);	
	}
}


?>